<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="询价书编号" prop="enquireNum">
      <el-input v-model="dataForm.enquireNum" placeholder="询价书编号"></el-input>
    </el-form-item>
    <el-form-item label="询价书名称" prop="enquireName">
      <el-input v-model="dataForm.enquireName" placeholder="询价书名称"></el-input>
    </el-form-item>
    <el-form-item label="单位" prop="company">
      <el-input v-model="dataForm.company" placeholder="单位"></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="linkman">
      <el-input v-model="dataForm.linkman" placeholder="联系人"></el-input>
    </el-form-item>
    <el-form-item label="联系地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="联系地址"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="联系传真" prop="fax">
      <el-input v-model="dataForm.fax" placeholder="联系传真"></el-input>
    </el-form-item>
    <el-form-item label="邮编" prop="zip">
      <el-input v-model="dataForm.zip" placeholder="邮编"></el-input>
    </el-form-item>
    <el-form-item label="电子邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
    </el-form-item>
    <el-form-item label="报价截止时间" prop="endDate">
      <el-input v-model="dataForm.endDate" placeholder="报价截止时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          enquireNum: '',
          enquireName: '',
          company: '',
          linkman: '',
          address: '',
          phone: '',
          fax: '',
          zip: '',
          email: '',
          endDate: '',
          remark: ''
        },
        dataRule: {
          enquireNum: [
            { required: true, message: '询价书编号不能为空', trigger: 'blur' }
          ],
          enquireName: [
            { required: true, message: '询价书名称不能为空', trigger: 'blur' }
          ],
          company: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ],
          linkman: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '联系地址不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          fax: [
            { required: true, message: '联系传真不能为空', trigger: 'blur' }
          ],
          zip: [
            { required: true, message: '邮编不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮箱不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '报价截止时间不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/enquire/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.enquireNum = data.enquire.enquireNum
                this.dataForm.enquireName = data.enquire.enquireName
                this.dataForm.company = data.enquire.company
                this.dataForm.linkman = data.enquire.linkman
                this.dataForm.address = data.enquire.address
                this.dataForm.phone = data.enquire.phone
                this.dataForm.fax = data.enquire.fax
                this.dataForm.zip = data.enquire.zip
                this.dataForm.email = data.enquire.email
                this.dataForm.endDate = data.enquire.endDate
                this.dataForm.remark = data.enquire.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/enquire/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'enquireNum': this.dataForm.enquireNum,
                'enquireName': this.dataForm.enquireName,
                'company': this.dataForm.company,
                'linkman': this.dataForm.linkman,
                'address': this.dataForm.address,
                'phone': this.dataForm.phone,
                'fax': this.dataForm.fax,
                'zip': this.dataForm.zip,
                'email': this.dataForm.email,
                'endDate': this.dataForm.endDate,
                'remark': this.dataForm.remark
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
